import 'dart:async';

import 'package:app/model/user/login_user_info.dart';
import 'package:app/page/container/home/base_home_view_builder.dart';
import 'package:app/page/container/home/desktop/desktop_home_view_builder.dart';
import 'package:app/page/container/home/mobile/mobile_home_view_builder.dart';
import 'package:app/page/container/home/page_home_model.dart';
import 'package:app/page/container/home/tablet/tablet_home_view_builder.dart';
import 'package:app/responsive.dart';
import 'package:app/utils/event_bus_utils.dart';
import 'package:flutter/cupertino.dart';

class PageHomeView extends StatefulWidget {
  const PageHomeView({super.key});

  @override
  State<StatefulWidget> createState() {
    return PageHomeController();
  }
}

class PageHomeController extends State<PageHomeView> {
  DesktopHomewViewBuilder desktopHomewViewBuilder = DesktopHomewViewBuilder();
  MobileHomewViewBuilder mobileHomewViewBuilder = MobileHomewViewBuilder();
  TabletHomewViewBuilder tabletHomewViewBuilder = TabletHomewViewBuilder();
  PageHomeModel data = PageHomeModel();

  late StreamSubscription _eventBusFn;

  @override
  void initState() {
    super.initState();
    _eventBusFn =
        GEventBusUtils().getDefault().on<LoginUserInfo>().listen((event) {
      data.updateloginUserInfo = event;
      setState(() {});
    });
  }

  @override
  void dispose() {
    super.dispose();
    //取消订阅
    _eventBusFn.cancel();
  }

  @override
  Widget build(BuildContext context) {
    BasePageHomeViewBuilder currentBuilder;
    if (Responsive.isDesktop(context)) {
      currentBuilder = desktopHomewViewBuilder;
    } else if (Responsive.isTablet(context)) {
      currentBuilder = tabletHomewViewBuilder;
    } else if (Responsive.isMobile(context)) {
      currentBuilder = mobileHomewViewBuilder;
    } else {
      currentBuilder = desktopHomewViewBuilder;
    }
    return currentBuilder.build(context, data);
  }
}
